package com.openstackapi.ctds.imp;

import java.util.List;



import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient.OSClientV2;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.compute.Flavor;
import org.openstack4j.openstack.OSFactory;

import com.openstackapi.ctds.ResourceTemplateController;
import com.openstackapi.ctds.pojo.CreateResourceTemplateResp;
import com.openstackapi.ctds.pojo.DeleteResourceTemplateResp;

public class ResourceTemplateControllerImp implements ResourceTemplateController {

	@Override
	public CreateResourceTemplateResp createResourceTemplate(String resourceTemplateID, String resourceTemplateXml) {
		
		
		//login and authentication 登录认证
		OSClientV2 os = OSFactory.builderV2()
		                       .endpoint("http://10.50.0.11:5000/v2.0")
		                       .credentials("admin","ADMIN_PASS")
		                       .tenantName("admin")
		                       .authenticate();
	
		
		//create template
		Flavor flavor = Builders.flavor()
                .name("Large Resources Template")
                .ram(4096)
                .vcpus(6)
                .disk(120)
                .swap(10)
                .rxtxFactor(1.11f)
                .build();

		 Flavor f = os.compute().flavors().create(flavor);
		
		
		//initialize the create template
		CreateResourceTemplateResp fs = new CreateResourceTemplateResp();
				
		
		return null;
	}

	@Override
	public DeleteResourceTemplateResp DeleteResourceTemplate(String resourceTemplateID) {
		//login and authentication֤
		OSClientV2 os = OSFactory.builderV2()
		                       .endpoint("http://10.50.0.11:5000/v2.0")
		                       .credentials("admin","ADMIN_PASS")
		                       .tenantName("admin")
		                       .authenticate();
		
		//initialize the delete class
		DeleteResourceTemplateResp faultstring = new DeleteResourceTemplateResp();
		
		//delete flavor by id
		ActionResponse resp = os.compute().flavors().delete(resourceTemplateID);
		System.out.println(resp.isSuccess());
		 
		return faultstring;
	}

	
	
	//test 测试
	public static void main(String[] args){

		
		OSClientV2 os = OSFactory.builderV2()
		                       .endpoint("http://10.50.0.11:5000/v2.0")
		                       .credentials("admin","ADMIN_PASS")
		                       .tenantName("admin")
		                       .authenticate();
		
		System.out.println("authentication complate \n"+ "start your project");
		
		Flavor flavor = Builders.flavor()
                .name("baater3 Resources Template")
                .ram(1024)
                .vcpus(1)
                .disk(10)
                .build();
		os.compute().flavors().create(flavor);
		
		System.out.println(os.compute().flavors().create(flavor));
		
		
		
		
		
//		System.out.println("operation complate");
//		System.out.println(flavor);
//		String id = flavor.getId();
//		
//		ActionResponse resp = os.compute().flavors().delete(id);
//		System.out.println(resp.isSuccess());
	}
}
